package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.Sound;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.web.multipart.MultipartFile;

@DubboService
public class PeachblossomImpl implements PeachblossomApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IdWorker idWorker;

    @Override
    public void savePeachbossom(Sound sound) {
        Long sid = idWorker.getNextId("sid");
        sound.setSid(sid);
        mongoTemplate.save(sound);
    }

    @Override
    public Sound getPeachblossom() {
        //使用统计函数，随机获取推荐的语音
        TypedAggregation<Sound> newAggregation = TypedAggregation.newAggregation(Sound.class,
                //Aggregation.match(),//指定查询条件
                Aggregation.sample(1)
        );
        AggregationResults<Sound> results = mongoTemplate.aggregate(newAggregation, Sound.class);
        //4、构造返回
        return results.getUniqueMappedResult();
    }
}
